 //
//  WSNavViewcontroller.m
//  DingLingDoctor
//
//  Created by 耿文康 on 16/3/8.
//  Copyright © 2016年 耿文康. All rights reserved.
//

#import "GGNavViewcontroller.h"
#import <UIKit/UIKit.h>
#import "GGNavgationBar.h"
@implementation GGNavViewcontroller

+ (void)initialize {
    //统一控制导航条的背景图片
    UINavigationBar *bar = [UINavigationBar appearance];
    NSDictionary *textAttributesDic = @{
                                        NSForegroundColorAttributeName:HEXColor(0xffffff, 1),
                                        NSFontAttributeName:[UIFont systemFontOfSize:19]
                                        };
    [bar setTitleTextAttributes:textAttributesDic];
    bar.barStyle = UIBarStyleDefault;
    //设置导航条不透明
    bar.translucent = NO;
    
    //获取Navigation Bar的位置和大小
    [bar setBackgroundImage:[[DefaultTools sharedInstance] imageMakeWithRect:CGRectMake(0, 0, SCREEN_WIDTH, 64) Color:HEXColor(0x000000, 1)]
             forBarPosition:UIBarPositionAny
                 barMetrics:UIBarMetricsDefault];
    [bar setShadowImage:[UIImage new]];
    //设置导航按钮的样式
    UIBarButtonItem *item = [UIBarButtonItem appearance];
    
    //设置Item字体颜色和大小
    [item setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:HEXColor(0xffffff, 1)} forState:UIControlStateNormal];

}


- (UIImageView *)getLineViewInNavigationBar:(UIView *)view
{
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self getLineViewInNavigationBar:subview];
        if (imageView) {
            return imageView;
        }
    }
    
    return nil;
}
//重写导航控制器的push方法,在push之前,设置目标控制器的自定义后退按钮 隐藏tabBAr
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    if (self.viewControllers.count != 0) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:animated];
    // 如果是第一个就只返回
    if (self.viewControllers.count < 2) {
        return;
    }
    // 如果覆盖了系统提供的后退按钮,手势返回上一个控制器
    self.interactivePopGestureRecognizer.delegate = (id)self;
    // 设置自定义后退按钮
    UIButton *backButton = [[UIButton alloc] init];
//    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"icBigbackNor"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"icBigbackNor"] forState:UIControlStateHighlighted];
    [backButton setTitleColor:HEXColor(0xffffff, 1) forState:UIControlStateNormal];
    backButton.titleLabel.font = [UIFont systemFontOfSize:15];
    
    // 自适应尺寸
    [backButton sizeToFit];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    // 调整间距
    //    UIBarButtonItem *seperator1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    //        seperator1.width = -10;
    //设置导航条上的item
    viewController.navigationItem.leftBarButtonItems = @[leftBarButtonItem];
}
-(void)dismissBack{
    [self dismissViewControllerAnimated:YES completion:nil];
}
// 返回方法
- (void)back{
//    [GGShowHUDTool dismiss];
    [self popViewControllerAnimated:YES];
}
-(BOOL)shouldAutorotate
{
    return NO;
    
}


-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    //return UIInterfaceOrientationMaskLandscapeRight;
    return self.orietation;
}

//- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
//{
//    return (interfaceOrientation != self.orietation);
//}
@end


